package cn.sdormitory.violative.form;

import cn.hutool.core.date.DatePattern;
import cn.sdormitory.basedata.service.BClassService;
import cn.sdormitory.common.PageForm;
import cn.sdormitory.common.utils.SpringUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.violative.entity.ViolationClassInterval;
import cn.sdormitory.violative.entity.ViolationPatrol;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@EqualsAndHashCode(callSuper = false)
@Data
public class ViolationClassIntervalQueryForm extends PageForm<ViolationClassIntervalQueryForm,ViolationClassInterval> {
    @ApiModelProperty("违纪编号")
    private Long id;

    @ApiModelProperty(value = "班级编号")
    private String classId;

    @ApiModelProperty(value = "违纪日期")
    @JsonFormat(pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
    @DateTimeFormat(pattern = DatePattern.NORM_DATE_PATTERN)
    private Date violationDay;

    private boolean queryImages = false;

    @ApiModelProperty("大于该状态")
    private boolean teacherReceiveQuery = false;

    @Override
    public QueryWrapper<ViolationClassInterval> query() {
        QueryWrapper<ViolationClassInterval> query = super.query();
        betweenDay(query,ViolationClassInterval::getViolationTime,violationDay);
        if(!queryImages){
            String imagesColumn = getColumn(ViolationClassInterval::getImages);
            query.select(info->!imagesColumn.equals(info.getColumn()));
        }
        if(teacherReceiveQuery){
            BClassService classService = SpringUtils.getBean(BClassService.class);
            Long userId = SysUserUtils.getSysUser().getId();
            Assert.notNull(userId,"请先登录");
            List<Long> classIds = classService.listClassIdsByTeacher(userId);
            query.in(getColumn(ViolationClassInterval::getClassId),classIds);
        }
        return query;
    }
}
